<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-box">
        <div class="register-header">
          <router-link to="/" class="logo">
            <img src="@/assets/giftflower.webp" alt="Logo" />
          </router-link>
          <h2>创建账户</h2>
          <p>加入优雅花艺，开启美好生活</p>
        </div>

        <form @submit.prevent="handleSubmit" class="register-form">
          <div class="form-group">
            <label>
              <i class="fas fa-user"></i>
              <input
                type="text"
                v-model="form.username"
                placeholder="请输入用户名"
                required
              />
            </label>
          </div>

          <div class="form-group">
            <label>
              <i class="fas fa-mobile-alt"></i>
              <input
                type="tel"
                v-model="form.phone"
                placeholder="请输入手机号"
                required
              />
            </label>
          </div>

          <div class="form-group verification">
            <label>
              <i class="fas fa-shield-alt"></i>
              <input
                type="text"
                v-model="form.code"
                placeholder="请输入验证码"
                required
              />
            </label>
            <button
              type="button"
              class="send-code"
              @click="sendCode"
              :disabled="countdown > 0"
            >
              {{ countdown > 0 ? `${countdown}s后重新发送` : "发送验证码" }}
            </button>
          </div>

          <div class="form-group">
            <label>
              <i class="fas fa-lock"></i>
              <input
                :type="showPassword ? 'text' : 'password'"
                v-model="form.password"
                placeholder="请设置密码"
                required
              />
              <button
                type="button"
                class="toggle-password"
                @click="showPassword = !showPassword"
              >
                <i
                  :class="['fas', showPassword ? 'fa-eye-slash' : 'fa-eye']"
                ></i>
              </button>
            </label>
          </div>

          <div class="form-group">
            <label>
              <i class="fas fa-lock"></i>
              <input
                :type="showConfirmPassword ? 'text' : 'password'"
                v-model="form.confirmPassword"
                placeholder="请确认密码"
                required
              />
              <button
                type="button"
                class="toggle-password"
                @click="showConfirmPassword = !showConfirmPassword"
              >
                <i
                  :class="[
                    'fas',
                    showConfirmPassword ? 'fa-eye-slash' : 'fa-eye',
                  ]"
                ></i>
              </button>
            </label>
          </div>

          <div class="agreement">
            <label class="checkbox">
              <input type="checkbox" v-model="form.agreement" required />
              <span>我已阅读并同意</span>
            </label>
            <a href="#" class="terms">《用户协议》</a>和
            <a href="#" class="privacy">《隐私政策》</a>
          </div>

          <button type="submit" class="submit-btn" :disabled="loading">
            {{ loading ? "注册中..." : "立即注册" }}
          </button>

          <div class="divider">
            <span>其他注册方式</span>
          </div>

          <div class="social-login">
            <button type="button" class="social-btn wechat">
              <i class="fab fa-weixin"></i>
            </button>
            <button type="button" class="social-btn qq">
              <i class="fab fa-qq"></i>
            </button>
            <button type="button" class="social-btn weibo">
              <i class="fab fa-weibo"></i>
            </button>
          </div>
        </form>

        <div class="login-link">
          已有账号？<router-link to="/login">立即登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>
  
  <script>
export default {
  name: "RegisterView",
  data() {
    return {
      loading: false,
      showPassword: false,
      showConfirmPassword: false,
      countdown: 0,
      form: {
        username: "",
        phone: "",
        code: "",
        password: "",
        confirmPassword: "",
        agreement: false,
      },
    };
  },
  methods: {
    sendCode() {
      if (!/^1[3-9]\d{9}$/.test(this.form.phone)) {
        alert("请输入正确的手机号");
        return;
      }
      this.countdown = 60;
      const timer = setInterval(() => {
        this.countdown--;
        if (this.countdown <= 0) {
          clearInterval(timer);
        }
      }, 1000);
      console.log("发送验证码到：", this.form.phone);
    },
    async handleSubmit() {
      // 表单验证
      if (
        !this.form.username ||
        !this.form.phone ||
        !this.form.password ||
        !this.form.confirmPassword
      ) {
        alert("请填写完整信息");
        return;
      }

      if (!/^1[3-9]\d{9}$/.test(this.form.phone)) {
        alert("请输入正确的手机号");
        return;
      }

      if (!this.form.code) {
        alert("请输入验证码");
        return;
      }

      if (this.form.password !== this.form.confirmPassword) {
        alert("两次输入的密码不一致");
        return;
      }

      if (!this.form.agreement) {
        alert("请阅读并同意用户协议");
        return;
      }

      try {
        this.loading = true;
        await this.$store.dispatch("register", {
          username: this.form.username,
          phone: this.form.phone,
          password: this.form.password,
        });
        alert("注册成功！请登录");
        this.$router.push("/login");
      } catch (error) {
        alert(error.message);
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>  
  <style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #fff6f6 0%, #ffe9e9 100%);
  padding: 40px 20px;
}

.register-container {
  width: 100%;
  max-width: 460px;
}

.register-box {
  background: white;
  border-radius: 10px;
  padding: 40px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  display: inline-block;
  margin-bottom: 20px;
}

.logo img {
}

.register-header h2 {
  font-size: 24px;
  color: #333;
  margin-bottom: 10px;
}

.register-header p {
  color: #666;
  font-size: 14px;
}

.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group label {
  display: block;
  width: 100%;
}

.form-group i {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  z-index: 1;
  pointer-events: none;
}

.form-group input {
  width: 100%;
  height: 44px;
  padding: 0 45px;
  border: 1px solid #ddd;
  border-radius: 22px;
  font-size: 14px;
  transition: all 0.3s;
}

.form-group input:focus {
  border-color: #ff6b6b;
  box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.1);
  outline: none;
}

.verification {
  display: flex;
  gap: 10px;
}

.verification .form-group {
  flex: 1;
}

.verification input {
  padding-right: 15px;
}

.send-code {
  height: 44px;
  padding: 0 20px;
  border: none;
  background: #ff6b6b;
  color: white;
  border-radius: 22px;
  font-size: 14px;
  white-space: nowrap;
  transition: background 0.3s;
}

.send-code:hover:not(:disabled) {
  background: #ff5252;
}

.send-code:disabled {
  background: #ffb3b3;
  cursor: not-allowed;
}

.toggle-password {
  position: absolute;
  right: 15px;
  top: 0;
  height: 44px;
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  padding: 0 15px;
  display: flex;
  align-items: center;
}

.agreement {
  margin-bottom: 20px;
  font-size: 14px;
  color: #666;
}

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.checkbox input {
  width: 16px;
  height: 16px;
  accent-color: #ff6b6b;
}

.terms,
.privacy {
  color: #ff6b6b;
  text-decoration: none;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background: #ff6b6b;
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.submit-btn:hover:not(:disabled) {
  background: #ff5252;
}

.submit-btn:disabled {
  background: #ffb3b3;
  cursor: not-allowed;
}

.divider {
  text-align: center;
  margin: 20px 0;
  position: relative;
}

.divider::before,
.divider::after {
  content: "";
  position: absolute;
  top: 50%;
  width: calc(50% - 70px);
  height: 1px;
  background: #eee;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.divider span {
  background: white;
  padding: 0 15px;
  color: #999;
  font-size: 14px;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 20px;
}

.social-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  transition: transform 0.3s;
}

.social-btn:hover {
  transform: scale(1.1);
}

.social-btn.wechat {
  background: #07c160;
}

.social-btn.qq {
  background: #12b7f5;
}

.social-btn.weibo {
  background: #e6162d;
}

.login-link {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.login-link a {
  color: #ff6b6b;
  text-decoration: none;
  margin-left: 5px;
}

@media (max-width: 480px) {
  .register-box {
    padding: 30px 20px;
  }

  .verification {
    flex-direction: column;
  }

  .send-code {
    width: 100%;
    padding: 12px;
  }
}
</style>
